<template>
  <div class="radio-bar-box1 flex">
    <span class="radio-bar-label">{{ label }}</span>
    <ul class="flex1">
      <li
        class="fl mr10"
        :class="value == item[props.value] ? 'li-active' : ''"
        v-for="(item,index) in data"
        :key="index"
        @click="handleChange(item)"
      >{{ item[props.label] }}
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  name: 'radioBar',
  model: {
    prop: 'value',
    event: 'change'
  },
  props: {
    value: {
      type: [String, Number]
    },
    label: {
      type: String,
      default: ''
    },
    data: {
      type: Array,
      default: () => {
        return []
      }
    },
    props: {
      type: Object,
      default: () => {
        return {
          label: 'baseName',
          value: 'baseId'
        }
      }
    }
  },
  data() {
    return {
      current: this.value
    }
  },
  watch: {
    value(val) {
      this.current = val
    },
    current(val) {
      this.$emit('change', val)
    }
  },
  methods: {
    handleChange(item) {
      let index = item[this.props.value]
      this.current = index

      // let ids = this.data.map(obj => {
      //   return obj[this.props.value]
      // })
      //
      // if (ids.indexOf('') != -1) {
      //   this.current = index
      // } else {
      //   if (this.current == index) {
      //     this.current = ''
      //   } else {
      //     this.current = index
      //   }
      // }
    }
  }
}
</script>

<style scoped lang="scss">

.radio-bar-box1 {

  .radio-bar-label {
    height: 30px;
    line-height: 30px;
    padding-right: 10px;
    color: #666666;
    font-size: 14px;
    box-sizing: border-box;
  }

  ul {
    margin: 0;
    padding: 0;

    li {
      height: 30px;
      line-height: 30px;
      padding: 0 10px;
      cursor: pointer;
      color: #333333;
      font-size: 14px;
      box-sizing: border-box;

      &:hover {
        color: #3184FF;
      }
    }

    .li-active {
      color: #3184FF;
      background-color: #F5F9FF;
      border-radius: 4px;

      &:hover {
        color: #3184FF;
      }
    }

  }
}

</style>
